package utils;

import java.sql.Connection;
import java.sql.SQLException;

public class ConnUtil {

    private static ThreadLocal<Connection> threadLocal = new ThreadLocal<>();


    public static Connection getConn() throws SQLException {

        Connection connect = threadLocal.get();
        if (connect == null) {
            connect = DruidUtil.getConnect();
            threadLocal.set(connect);
        }
        return threadLocal.get();
    }

    public static void closeConn() throws SQLException {

        Connection connect = threadLocal.get();
        if (connect == null) {
            return;
        }
        if (!connect.isClosed()) {
            connect.close();
            threadLocal.set(null);
        }
    }
}
